//
//  SearchStaffViewController.m
//  APJ Resource
//
//  Created by Richard Zhao on 1/21/14.
//  Copyright (c) 2014 APJ. All rights reserved.
//

#import "SearchStaffViewController.h"
#import "StaffListViewController.h"
#import "DeptPicker.h"
#import "SkillCategoryPicker.h"
#import "HRStore.h"
#import "Staff.h"

@interface SearchStaffViewController ()
{
    int selectedDeptId;
}
@end

@implementation SearchStaffViewController

@synthesize selectedSkills;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        UITabBarItem *item = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemSearch tag:1];
        [self setTabBarItem:item];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg"]]];
    [[self navigationItem] setTitle:@"人员查询"];
    
    selectedSkills = [NSMutableArray array];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)search:(id)sender
{
    NSArray *result = [[HRStore sharedStore] findStaffAboutName:searchName.text
                                                         inDept:selectedDeptId
                                                     withSkills:selectedSkills];
    for (Staff *staff in result) {
        NSLog(@"%@", staff.chineseName);
    }
    
    if (result.count > 0) {
        StaffListViewController *staffList = [[StaffListViewController alloc] init];
        [staffList setDataSource:result];
        [staffList setTitle:@"人员查询结果"];
        [self.navigationController pushViewController:staffList animated:YES];
    }

}

- (IBAction)pickDept:(id)sender {
    CGRect rect = [[self view] convertRect:[sender bounds] fromView:sender];
    DeptPicker *picker = [[DeptPicker alloc] init];
    [picker setSelectedDeptId:selectedDeptId];
    [picker setSelectedDeptShortName:searchDept.titleLabel.text];
    deptPop = [[UIPopoverController alloc] initWithContentViewController:picker];
    [picker setPopView:deptPop];
    [deptPop setDelegate:self];
    [deptPop setPopoverContentSize:CGSizeMake(300, 600)];
    [deptPop presentPopoverFromRect:rect inView:[self view]
           permittedArrowDirections:UIPopoverArrowDirectionLeft
                           animated:YES];
    
    
}

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
    if (popoverController == deptPop) {
        DeptPicker *picker = (DeptPicker *)popoverController.contentViewController;
        [searchDept setTitle:picker.selectedDeptShortName forState:UIControlStateNormal];
        selectedDeptId = picker.selectedDeptId;
    }
    
    if (popoverController == skillPop) {
        NSMutableString *skills = [[NSMutableString alloc] init];
        for (NSString *skill in selectedSkills) {
            if (![skills isEqualToString:@""]) {
                [skills appendString:@"; "];
            }
            [skills appendString:skill];
        }
        [searchSkill setTitle:skills forState:UIControlStateNormal];
    }
 
    [popoverController dismissPopoverAnimated:YES];
    popoverController = nil;
}

- (IBAction)pickSkill:(id)sender {
    CGRect rect = [[self view] convertRect:[sender bounds] fromView:sender];
    SkillCategoryPicker *picker = [[SkillCategoryPicker alloc] init];
    [picker setSelectedSkills:selectedSkills];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:picker];
    skillPop = [[UIPopoverController alloc] initWithContentViewController:nav];
    [picker setPopView:skillPop];
    [skillPop setDelegate:self];
    [skillPop setPopoverContentSize:CGSizeMake(200, 600)];
    [skillPop presentPopoverFromRect:rect inView:[self view]
                    permittedArrowDirections:UIPopoverArrowDirectionAny
                                    animated:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [searchName resignFirstResponder];
    return YES;
}

@end
